/*
* Copyright 2011 Joshua Carbonneau
*
* This file is part of Texture Chaos.
*
* Texture Chaos is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Texture Chaos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Texture Chaos.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "include/tilemap.h"
#include "include/chaos.h"

/******************************************************
* Create a new TileMap from the given config file     *
* filename: The config file to use in the new TileMap *
* returns: A pointer to the new TileMap               *
*******************************************************/
struct TileMap* newTileMap(char* filename)
{
    // Seed the random number generator
    srandom(getpid());

    FILE* infile = fopen(filename, "r");

    // Allocate memory
    struct TileMap* tm = (struct TileMap*)malloc(sizeof(struct TileMap));

    // Read in the header from the file
    fscanf(infile, "%d %d\n", &tm->tileHeight, &tm->tileWidth);
    fscanf(infile, "%d %d\n", &tm->numTilesY, &tm->numTilesX);

    // Allocate memory for the grid
    tm->grid = (int**)malloc(sizeof(int*) * tm->numTilesY);
    int y, x;
    for(y=0; y<tm->numTilesY; y++)
        tm->grid[y] = (int*)malloc(sizeof(int) * tm->numTilesX);
    
    // Read in the grid from the file
    for(y=0; y<tm->numTilesY; y++)
        for(x=0; x<tm->numTilesX; x++)
            fscanf(infile, "%d", &tm->grid[y][x]);
    fclose(infile);

    // Output some info
    debug(2, "Tile size: %d x %d\n", tm->tileHeight, tm->tileWidth);
    debug(2, "Image size (in tiles): %d x %d\n", tm->numTilesY, tm->numTilesX);

    // Output the grid contents
    debug(2, "Contents of TileMap:\n");
    for(y=0; y<tm->numTilesY; y++)
    {
        for(x=0; x<tm->numTilesX; x++)
        {
            debug(2, "%d ", tm->grid[y][x]);
        }
        debug(2, "\n");
    }

    return tm;
}

/********************************
* Deallocate a TileMap          *
* tm: The TileMap to deallocate *
*********************************/
void destroyTileMap(struct TileMap* tm)
{
    int y;
    for(y=0; y<tm->numTilesY; y++)
        free(tm->grid[y]);
    free(tm->grid);
    free(tm);
}

/******************************************************************************
* Get a random tile index (in pixel coordinates) from a TileMap               *
* tm: The TileMap to use                                                      *
* tly: The y coordinate of the top right corner of the randomly chosen tile   *
* tlx: The x coordinate of the top right corner of the randomly chosen tile   *
* bry: The y coordinate of the bottom left corner of the randomly chosen tile *
* brx: The x coordinate of the bottom left corner of the randomly chosen tile *
*******************************************************************************/
void getRandomTileCoord(struct TileMap* tm, int* tly, int* tlx, int* bry, int* brx)
{
    // Choose a random tile index
    int ty = random() % tm->numTilesY;
    int tx = random() % tm->numTilesX;

    // Check the grid, and rechoose if the tile index is invalid
    // (This is the inefficient part)
    while(tm->grid[ty][tx] == 0)
    {
        ty = random() % tm->numTilesY;
        tx = random() % tm->numTilesX;
    }

    // Convert the tile index to pixel coordinates
    *tly = ty * tm->tileHeight;
    *tlx = tx * tm->tileWidth;

    *bry = *tly + tm->tileHeight;
    *brx = *tlx + tm->tileWidth;
}
    

